package com.hongxin.nw.netty.down;

import com.hongxin.nw.netty.model.Transfer;
import com.hongxin.nw.netty.model.packing.ASDUcsdna1;
import com.hongxin.nw.netty.model.packing.info.Csdna1_5;
import com.hongxin.nw.netty.utils.FieldPack;
import com.hongxin.nw.netty.utils.pools.AsduStartPools;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;

/**
 * @remark 交易取随机数下行数据
 * @author: GuoXiaolin
 * @date 2019/6/21 17:27
 * @Version 1.0
 * @Descibe
 */
public class TranReq implements IChannelRequest {

    @Override
    public String doPacking(Transfer transfer)  throws Exception{
        String code = transfer.getCode();
        Csdna1_5 csdna1_5 = new Csdna1_5();
        csdna1_5 = csdna1_5.setCo(code)
                .setChargeIdentify("0")
                .setMainRandom("0");

        ASDUcsdna1 mm = new ASDUcsdna1();
        mm.setLogType(String.valueOf(TypeIdentif.RECORD_TRANSACTION.getInt()));
        mm.setSendData(csdna1_5.packing());

//        as.setType(TypeIdentif.IDENTIFY_C_SD_NA_1.getInt());
//        as.setVsq(StaticValue.DEFAULT_VSQ, StaticValue.SQ0);
//        as.setCot(TypeIdentif.COT_UNKNOWN_COT.getInt());
//        as.setInfoadr(0);
//        as.setStaadr(AsduStartPools.getAsduStart(code).getStatAddress());


        String as = FieldPack.packASDU(TypeIdentif.IDENTIFY_C_SD_NA_1.getInt(),
                TypeIdentif.COT_UNKNOWN_COT.getInt(),
                AsduStartPools.getAsduStart(code).getStatAddress(), 0,
                StaticValue.DEFAULT_VSQ, StaticValue.SQ0);

        String data = as + mm.getHexData();

//        APCI ac = new APCI();
//        ac.setIdentifyStart(TypeIdentif.IDENTIFY_START.getInt())
//                .setApduLength(as.getHexData().length()/2)
//                .setContarea(TypeIdentif.FORMAT_I.getStr(),StaticValue.U_START_V);
//
//        return ac.getHexData() + as.getHexData();

        String ac = FieldPack.packAPCI(data.length(), TypeIdentif.FORMAT_I.getStr(), StaticValue.U_START_V);
        return ac + data;
    }
}
